//////////////////////////
// 数据库基本操作promise化 //
//////////////////////////

import assert from 'assert-plus';

function proxy (schema, method, conditions, fields, options) {
	// assert.object(schema, 'mongo schema');
	assert.string(method, 'method');

	conditions = conditions || {};
	options = options || {};
	// console.log(schema, method, conditions, fields, options);

	switch(method) {
		case 'findOne':
		case 'findOneAndUpdate':
		case 'update':
		case 'find': 
			return new Promise((resolve, reject) => {
				schema[method](conditions, fields, options, (err, results) => {
					if (err) reject(err);
					else resolve(results);
				});
			});
		case 'create': 
		case 'count':
		case 'findOneAndRemove':
			return new Promise((resolve, reject) => {
				schema[method](conditions, (err, results) => {
					if (err) reject(err);
					else resolve(results);
				});
			});
	}
}

export default proxy;